<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('User_model');
    }
    
    public function index()
    {
        
    }
    
    public function add()
    {
        $params = array();
        if($this->input->is_post()) {
            $params = $this->input->post();
            if(empty($params['user_mid']) || empty($params['user_type']) || empty($params['first_name']) || empty($params['last_name']) || empty($params['password'])) {
                $params['error'] = true;
                $this->load->view('user/edit', $params);
            } else {
                $params['success'] = $this->User_model->add($params);
                $params['success'] = true;
                $this->load->view('user/add', $params);
            }
        } else {
            $this->load->view('user/add');
        }
    }
    
    public function edit()
    {
        $userId = (int)$this->input->get('user_id');
        
        $user = $this->User_model->getId($userId);
        if(empty($user['data'])) {
            $params['msg'] = 'Không tồn tại mẫu tin này trong hệ thống !';
            $this->load->view('error', $params);
        } else {
            $params = array();
            if($this->input->is_post()) {
                $params = $this->input->post();
                unset ($params['user_mid']);
                if(empty($params['user_id']) || empty($params['user_type']) || empty($params['first_name']) || empty($params['last_name']) || empty($params['password'])) {
                    $params['error'] = true;
                    $this->load->view('user/edit', $params);
                } else {
                    $params['success'] = $this->User_model->edit($params, $params['user_id']);
                    $params['success'] = true;
                    $user = $this->User_model->getId($userId);
                    $params = array_merge($user, $params);
                    $this->load->view('user/edit', $params);
                }
            } else {
                $this->load->view('user/edit', $user);
            }
        }
    }
    
    public function delete()
    {
        $rs = 0;
        $userId = $this->input->get('user_id');
        if($userId > 0) {
            
            $rs = (int)$this->User_model->delete($userId);
        }
        echo $rs;
    }
    
    public function login()
    {
        $error = false;
        if($this->input->is_post()) {
            $params = $this->input->post();
            if(empty($params['user_mid']) || empty($params['password'])) {
                $error = true;
            } else {
                $where = array(
                    'user_mid' => $params['user_mid'],
                    'password' => md5($params['password'])
                );
                $data = $this->User_model->selectList($where);
                if(!empty($data['total'])) {
                    // User logged
                    $this->session->set_userdata('user', $data['data'][0]);
                } else {
                    $error = true;
                }
            }
        }
        
        $this->load->view('user/login', $error);
    }
    
    public function logout()
    {
        $this->session->sess_destroy();
        redirect(base_url(), 'location', 301);
    }
}
?>
